package week02;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;

import java.io.IOException;
import java.util.Iterator;

public class Week02Reduce implements Reducer<Text, Week02OutputWritable, Text, Week02OutputWritable> {


    @Override
    public void reduce(Text text, Iterator<Week02OutputWritable> iterator, OutputCollector<Text, Week02OutputWritable> outputCollector, Reporter reporter) throws IOException {
        int upQuota = 0;
        int downQuota = 0;
        int totalQuota = 0;
        int checkTotal = 0;

        for(; iterator.hasNext();) {
            Week02OutputWritable week02OutputWritable = iterator.next();
            Week02Output week02Output = week02OutputWritable.get();

            upQuota += week02Output.getUpSize();
            downQuota += week02Output.getDownSize();
            totalQuota += week02Output.getTotalSize();
            checkTotal = upQuota + downQuota;

        }
        System.out.println(text + " totalQuota:" + totalQuota + " checkTotal:" + checkTotal);
        Week02Output week02Output = new Week02Output();
        week02Output.setMobile(text.toString());
        week02Output.setUpSize(upQuota);
        week02Output.setDownSize(downQuota);
        week02Output.setTotalSize(totalQuota);
        Week02OutputWritable output = new Week02OutputWritable(week02Output);

        outputCollector.collect(text, output);


    }

    @Override
    public void close() throws IOException {

    }

    @Override
    public void configure(JobConf jobConf) {

    }
}
